package com.auxgroup.smarthome.app.entity;

import com.alibaba.fastjson.annotation.JSONField;
import org.apache.commons.lang3.tuple.Pair;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.Table;
import java.io.Serializable;

/**
 * Created by kevinchen on 2017/9/13.
 * app端日月年的波峰波谷波平用电总量统计
 */
@Entity
@Table(name = "app_electricity_statistics",indexes = {@Index(columnList = "timeType",
        name = "index_app_electricity_statistics_timetype")})
public class AppElectricityStatisticsEntity implements Serializable {

    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(columnDefinition=" VARCHAR(36) NOT NULL COMMENT '主键'")
    private String id;

    /**
     * 只有符合机智云协议的家用空调设备用SAAS平台推送峰谷节电功能指令
     * 故这里的主键为机智云的did
     */
    @Column(columnDefinition = "varchar(50) NOT NULL COMMENT '机智云设备did'")
    private String did;

    @Column(columnDefinition = "VARCHAR(50) NOT NULL DEFAULT 0 COMMENT '机智云产品productKey'")
    private String pk;

    @Column(columnDefinition = "tinyint(1) NOT NULL DEFAULT 0 COMMENT '0:表示天、1:表示月、2:表示年'")
    private Integer timeType;

    @Column(columnDefinition = "varchar(20) NOT NULL COMMENT '天的格式：20170913、月的格式：201709、年的格式：2017'")
    private String timeStr;

    @Column(columnDefinition = "float NOT NULL DEFAULT 0.00 COMMENT '波峰总量'")
    private Float peakTotal;

    @Column(columnDefinition = "float NOT NULL DEFAULT 0.00 COMMENT '波谷总量'")
    private Float valleyTotal;

    /**
     * 只有每日用电曲线需用到，
     * 即timeType = 0 的时候有值，其他时候为null
     * 波峰时间段、波谷时间段在同一天有可能存在两个时间段
     * 若波峰有两个时间段，则波谷就只有一个时间段 故时间段格式为 num1,num2-num3,num4|num5,num6
     * 若波谷有两个时间段，则波峰就只有一个时间段 故时间段格式为 num1,num2|num3,num4-num5,num6
     * @return
     */
    @Column(columnDefinition = "varchar(50) NULL COMMENT '波峰波谷时间段，格式：10,15|0,6_21,23（即波峰时间段为10点-15点，波谷时间段为1点-6点,）日用电曲线需用到'")
    private String timeBucket;


    public String getDid() {
        return did;
    }

    public void setDid(String did) {
        this.did = did;
    }

    public String getPk() {
        return pk;
    }

    public void setPk(String pk) {
        this.pk = pk;
    }

    public Integer getTimeType() {
        return timeType;
    }

    public void setTimeType(Integer timeType) {
        this.timeType = timeType;
    }

    public String getTimeStr() {
        return timeStr;
    }

    public void setTimeStr(String timeStr) {
        this.timeStr = timeStr;
    }

    public Float getPeakTotal() {
        return peakTotal;
    }

    public void setPeakTotal(Float peakTotal) {
        this.peakTotal = peakTotal;
    }

    public Float getValleyTotal() {
        return valleyTotal;
    }

    public void setValleyTotal(Float valleyTotal) {
        this.valleyTotal = valleyTotal;
    }


    public String getTimeBucket() {
        return timeBucket;
    }

    public void setTimeBucket(String timeBucket) {
        this.timeBucket = timeBucket;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public AppElectricityStatisticsEntity() {
    }

    public AppElectricityStatisticsEntity(String pk,String did, Integer timeType, String timeStr) {
        this.pk = pk;
        this.did = did;
        this.timeType = timeType;
        this.timeStr = timeStr;
        this.peakTotal = 0f;
        this.valleyTotal = 0f;
    }

    public AppElectricityStatisticsEntity(String pk,String did, Integer timeType, String timeStr,
                                          Float peakTotal, Float valleyTotal,String timeBucket) {
        this.pk = pk;
        this.did = did;
        this.timeType = timeType;
        this.timeStr = timeStr;
        this.peakTotal = peakTotal;
        this.valleyTotal = valleyTotal;
        this.timeBucket = timeBucket;
    }

    public void addPeakTotal(Float peakValue){
        this.peakTotal += peakValue;
    }

    public void addValleyTotal(Float valleyValue){
        this.valleyTotal += valleyValue;
    }

    /**
     * -----日用电曲线------
     * 解析波峰波谷时间段
     * 波峰时间段、波谷时间段在同一天有可能存在两个时间段
     * 若波峰有两个时间段，则波谷就只有一个时间段 故时间段格式为 num1,num2_num3,num4|num5,num6
     * 若波谷有两个时间段，则波峰就只有一个时间段 故时间段格式为 num1,num2|num3,num4_num5,num6
     * @index 0:波峰 1：波谷
     * @return
     */
    @JSONField(serialize = false)
    public Pair<Pair<Integer,Integer>,Pair<Integer,Integer>> getParsePeakVallyBucket(int index){
        String[] split = timeBucket.split("\\|");
        if (split.length!=2){
            throw new IllegalArgumentException("波峰波谷时间段长度有问题！");
        }
        //波峰或波谷时间段解析
        Pair<Integer,Integer> pvBucket1 = null;
        Pair<Integer,Integer> pvBucket2 = null;
        String splitStr = split[index];
        String[] split1 = splitStr.split("_");
        if (split1.length >=1 && split1.length <=2 ){
            String pvStr = split1[0];
            String[] pvArr= pvStr.split(",");
            if (pvArr.length !=2){
                throw new IllegalArgumentException("时间段长度有问题！");
            }
            Integer pv1 = Integer.parseInt(pvArr[0]);
            Integer pv2 = Integer.parseInt(pvArr[1]);
            pvBucket1 = Pair.of(pv1,pv2);
        }
        if (split1.length== 2){
            String pv2Str = split1[1];
            String[] pvArr= pv2Str.split(",");
            if (pvArr.length !=2){
                throw new IllegalArgumentException("时间段长度有问题！");
            }
            Integer pv3 = Integer.parseInt(pvArr[0]);
            Integer pv4 = Integer.parseInt(pvArr[1]);
            pvBucket2 = Pair.of(pv3,pv4);
        }
        return Pair.of(pvBucket1,pvBucket2);
    }



}
